﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UCL;
using UCL.Data;

namespace UCL.Forms.Components
{
    /// <summary>
    /// SQL上下文提示控件
    /// </summary>
    /// <remarks>2014-1-21 by sutroon</remarks>
    public partial class frmSqlIntellisense : Form
    {
        public IDatabaseHelper DBH { get; set; }
        public string TableName { get; set; }
        public Action<string> ReceiveTextHandler { get; set; }
        public frmSqlIntellisense()
        {
            InitializeComponent();
            lbTables.SetLineHeight();
            lbFields.SetLineHeight();
            cmiClickConfig();
        }

        private void frmSqlIntellisense_Load(object sender, EventArgs e)
        {
            if (DBH == null)
            {
                MessageBox.Show("数据库连接无效");
                this.Close();
                return;
            }
            lbTables.DataSource = DBH.SO_GetTables();
            if(!string.IsNullOrEmpty(this.TableName)) lbTables.SelectedItem=this.TableName;
        }
        private void cmiClickConfig()
        {
            cmi按字母排序ToolStripMenuItem.Click += (object sender, EventArgs e) =>
            {
                lbFields.DataSource = null;
                if (cmi按字母排序ToolStripMenuItem.Text == "按字母排序(&A)")
                {
                    cmi按字母排序ToolStripMenuItem.Text = "取消按字母排序(&C)";
                    lbFields.Sorted = true;
                }
                else
                {
                    cmi按字母排序ToolStripMenuItem.Text = "按字母排序(&A)";
                    lbFields.Sorted = false;
                }
                lbFields.DataSource = DBH.SO_GetFields(lbTables.SelectedItem.ToString());
            };
        }

        private void lbTables_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbTables.SelectedIndex == -1) return;
            lbFields.DataSource=DBH.SO_GetFields(lbTables.SelectedItem.ToString());
        }

        private void lbFields_SelectedIndexChanged(object sender, EventArgs e)
        {
            tbText.Text = string.Format("{0}", lbFields.SelectedItem);
        }
        private void Run(string s)
        {
            if (ReceiveTextHandler != null) ReceiveTextHandler(s);
            this.Close();
        }

        private void lbTables_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter) this.Run(lbTables.SelectedItem.ToString());
            if (e.KeyChar == (char)Keys.Escape) this.Close();
        }

        private void lbFields_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter) this.Run(GetFields());
            if (e.KeyChar == (char)Keys.Escape) this.Close();
        }

        private void lbTables_DoubleClick(object sender, EventArgs e)
        {
            this.Run(lbTables.SelectedItem.ToString());
        }

        private void lbFields_DoubleClick(object sender, EventArgs e)
        {
            this.Run(GetFields());
        }
        private string GetFields()
        {
            StringBuilder sb = new StringBuilder();
            foreach (object i in lbFields.SelectedItems)
            {
                sb.AppendFormat(" {0},", i);
            }
            return sb.Length > 0 ? sb.ToString().Trim(',',' ') : string.Empty;
        }

    }
}
